home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacAddict 123
/
MacAddict_123_2006_11.iso
/
Software
/
Productivity
/
iClip lite 2.wdgt
/
js
/
scrollbar.js
< prev
Wrap
Text File
|
2006-08-05
|
8KB
|
188 lines
/* Copyright © 2006, Inventive. */
/* classes */
function Scrollbar(bar, area, horizontal, margin, padding) {
/* events */
var self = this;
addEvent(window, "mousemove", false, function(event) {
if (self.dragging) self.scrollBar((self.horizontal ? event.clientX : event.clientY) - self.offset - self.click);
});
addEvent(window, "mouseup", false, function(event) {
if (self.dragging) {
self.scrollArea(Math.round((self.horizontal ? self.area.scrollLeft : self.area.scrollTop) / 102) * 102);
self.dragging = false;
}
self.tracking.on = false;
if (self.tracking.timer != null) {
clearInterval(self.tracking.timer);
self.tracking.timer = null;
}
});
addEvent(window, "keypress", false, function(event) {
if (self.focused && !(event.shiftKey || event.metaKey || event.altKey || event.ctrlKey)) {
var handled = true;
switch (event.keyIdentifier) {
case "Up":
if (!horizontal) self.scrollArea(self.area.scrollTop - 10);
break;
case "Down":
if (!horizontal) self.scrollArea(self.area.scrollTop + 10);
break;
case "Left":
if (horizontal) self.scrollArea(self.area.scrollLeft - 102);
break;
case "Right":
if (horizontal) self.scrollArea(self.area.scrollLeft + 102);
break;
case "PageUp":
self.scrollArea(self.horizontal ? self.area.scrollLeft - self.area.clientWidth : self.area.scrollTop - self.area.clientHeight);
break;
case "PageDown":
self.scrollArea(self.horizontal ? self.area.scrollLeft + self.area.clientWidth : self.area.scrollTop + self.area.clientHeight);
break;
case "Home":
self.scrollBar(0);
break;
case "End":
self.scrollBar(self.max);
break;
default:
handled = false;
}
if (handled) event.stopPropagation();
}
});
addEvent(area, "mouseover", false, function(event) { self.focused = true; });
addEvent(area, "mouseout", false, function(event) { self.focused = false; });
addEvent(area, "mousewheel", false, function(event) {
var delta = self.horizontal ? event.wheelDelta : 0 - event.wheelDelta;
var deltaScroll = delta / 120 * 50;
var binsToScroll = Math.round(deltaScroll / 102);
self.scrollArea((self.horizontal ? self.area.scrollLeft : self.area.scrollTop) - (binsToScroll * 102));
});
addEvent(bar, "mouseover", false, function(event) { self.focused = true; });
addEvent(bar, "mouseout", false, function(event) { self.focused = false; });
addEvent(bar, "mousedown", false, function(event) {
self.tracking.on = true;
if (scrollTracking(event)) {
self.tracking.timer = setInterval(function() {
if (scrollTracking(event)) {
clearInterval(self.tracking.timer);
startTracking(event);
}
}, 500);
}
});
addEvent(bar, "mousemove", false, function(event) {
if (self.tracking.on && self.tracking.timer == null)
startTracking(event);
});
addEvent(bar, "mousewheel", false, function(event) {
var delta = self.horizontal ? event.wheelDelta : 0 - event.wheelDelta;
var deltaScroll = delta / 120 * 50;
var binsToScroll = Math.round(deltaScroll / 102);
self.scrollArea((self.horizontal ? self.area.scrollLeft : self.area.scrollTop) - (binsToScroll * 102));
});
addEvent(bar.childNodes[0], "mousedown", false, function(event) { event.preventDefault(); });
addEvent(bar.childNodes[1], "mousedown", false, function(event) { event.preventDefault(); });
addEvent(bar.childNodes[2], "mousedown", false, function(event) { event.preventDefault(); });
addEvent(bar.childNodes[3], "mousedown", false, function(event) {
self.click = (self.horizontal ? event.clientX : event.clientY) - self.offset - self.position;
self.dragging = true;
event.stopPropagation();
});
addEvent(bar.childNodes[3].childNodes[0], "mousedown", false, function(event) { event.preventDefault(); });
addEvent(bar.childNodes[3].childNodes[1], "mousedown", false, function(event) { event.preventDefault(); });
addEvent(bar.childNodes[3].childNodes[2], "mousedown", false, function(event) { event.preventDefault(); });
/* functions */
function startTracking(event) {
self.tracking.timer = setInterval(function() {
if (!scrollTracking(event)) {
clearInterval(self.tracking.timer);
self.tracking.timer = null;
}
}, 125);
}
function scrollTracking(event) {
self.click = (self.horizontal ? event.clientX : event.clientY) - self.offset;
if (self.click >= self.position && self.click <= self.position + self.widgetSize)
return false;
if (self.click < self.position)
self.scrollArea(self.horizontal ? self.area.scrollLeft - self.area.clientWidth : self.area.scrollTop - self.area.clientHeight);
else
self.scrollArea(self.horizontal ? self.area.scrollLeft + self.area.clientWidth : self.area.scrollTop + self.area.clientHeight);
return true;
}
/* variables */
this.bar = bar;
this.area = area;
this.horizontal = horizontal;
this.margin = margin * 2;
this.padding = padding;
this.offset = margin + 83;
this.scrollRatio = 0;
this.trackSize = 0;
this.trackRatio = 0;
this.widgetSize = 0;
this.max = 0;
this.click = 0;
this.position = 0;
this.tracking = {on: false, timer: null};
this.focused = false;
/* code */
//var node = bar;
//do {
// if (this.horizontal)
// if (node.offsetLeft != null) this.offset += node.offsetLeft;
// else
// if (node.offsetTop != null) this.offset += node.offsetTop;
// node = node.parentNode;
//}
//while (node != null);
this.refresh();
}
Scrollbar.prototype.refresh = function() {
this.scrollRatio = this.horizontal ? this.area.scrollWidth / this.area.clientWidth : this.area.scrollHeight / this.area.clientHeight;
this.trackSize = (this.horizontal ? this.area.clientWidth : this.area.clientHeight) - this.margin;
this.widgetSize = this.trackSize / this.scrollRatio;
this.trackRatio = ((this.horizontal ? this.area.clientWidth : this.area.clientHeight) - this.widgetSize) / (this.trackSize - Math.max(this.widgetSize, 24));
if (this.widgetSize < 24) this.widgetSize = 24;
this.max = this.trackSize - this.widgetSize;
this.bar.style.display = this.widgetSize < this.trackSize ? "block" : "none";
if (this.horizontal) {
this.area.style.paddingBottom = this.widgetSize < this.trackSize ? ((this.padding + 17) / 10) + "em" : (this.padding / 10) + "em";
this.bar.childNodes[1].style.width = ((this.trackSize - 28) / 10) + "em";
this.bar.childNodes[3].childNodes[1].style.width = ((this.widgetSize - 24) / 10) + "em";
}
else {
this.area.style.paddingRight = this.widgetSize < this.trackSize ? ((this.padding + 17) / 10) + "em" : (this.padding / 10) + "em";
this.bar.childNodes[1].style.height = ((this.trackSize - 28) / 10) + "em";
this.bar.childNodes[3].childNodes[1].style.height = ((this.widgetSize - 24) / 10) + "em";
}
this.scrollArea(this.horizontal ? this.area.scrollLeft : this.area.scrollTop);
//this.area.innerHTML = this.area.innerHTML;
}
Scrollbar.prototype.scrollBar = function(to) {
if (to < 0) this.position = 0;
else if (to > this.max) this.position = this.max;
else this.position = to;
if (this.horizontal) {
this.area.scrollLeft = (this.position * this.trackRatio) * this.scrollRatio;
this.bar.childNodes[3].style.left = (this.position / 10) + "em";
}
else {
this.area.scrollTop = (this.position * this.trackRatio) * this.scrollRatio;
this.bar.childNodes[3].style.top = (this.position / 10) + "em";
}
}
Scrollbar.prototype.scrollArea = function(to) {
this.scrollBar((to / this.scrollRatio) / this.trackRatio);
}